import cv2
image=cv2.imread("hat.png",cv2.IMREAD_GRAYSCALE)
cv2.imshow('Input',image)
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))

eroded_img1=cv2.erode(image,kernel)
cv2.imshow("Eroded(5*5)",eroded_img1)
dilated_img1=cv2.dilate(image,kernel,iterations=1)
cv2.imshow("1-dilate(5*5)",dilated_img1)
cv2.waitKey()

image1=cv2.erode(image,kernel)
image2=cv2.dilate(image1,kernel)
cv2.imshow("Output1",image2)
image3=cv2.dilate(image,kernel)
image4=cv2.erode(image3,kernel)
cv2.imshow("Output2",image4)
cv2.waitKey()

gradient=cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kernel)
cv2.imshow("Gradient",gradient)
cv2.waitKey()

tophat=cv2.morphologyEx(image,cv2.MORPH_TOPHAT,kernel)
blackhat=cv2.morphologyEx(image,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow('TopHat',tophat)
cv2.imshow("BlackHat",blackhat)

cv2.waitKey()
cv2.destroyAllWindows()